﻿
// Modules/ViewModels/RobotParameterViewModel.cs
using CommunityToolkit.Mvvm.ComponentModel;
using System.Collections.ObjectModel;
using Deepseek_Robot.Modules.Models;
using ClosedXML.Excel;
using System.IO;
using static Deepseek_Robot.Modules.Models.ABBDataModel;

namespace Deepseek_Robot.Modules.ViewModels
{
    public partial class RobotParameterViewModel : ObservableObject
    {
        [ObservableProperty]
        private ObservableCollection<RobTargetDataModel> robTargetData;

        [ObservableProperty]
        private ObservableCollection<ThreeDataModel> threeData;

        [ObservableProperty]
        private ObservableCollection<DataTypeArrayModel> dataTypeArray;

        public RobotParameterViewModel()
        {
            // 初始化数据
            LoadData();
        }

        private void LoadData()
        {
            RobTargetData = new ObservableCollection<RobTargetDataModel>();
            ThreeData = new ObservableCollection<ThreeDataModel>();
            DataTypeArray = new ObservableCollection<DataTypeArrayModel>();
        }

        // 导出数据到 Excel
        public void ExportToExcel()
        {
            using (var workbook = new XLWorkbook())
            {
                // 导出 RobTargetData
                var robTargetSheet = workbook.Worksheets.Add("RobTargetData");
                robTargetSheet.Cell(1, 1).InsertTable(RobTargetData);

                // 导出 ThreeData
                var threeDataSheet = workbook.Worksheets.Add("ThreeData");
                threeDataSheet.Cell(1, 1).InsertTable(ThreeData);

                // 导出 DataTypeArray
                var dataTypeArraySheet = workbook.Worksheets.Add("DataTypeArray");
                dataTypeArraySheet.Cell(1, 1).InsertTable(DataTypeArray);

                // 保存文件
                workbook.SaveAs("RobotParameters.xlsx");
            }
        }

        // 从 Excel 导入数据
        public void ImportFromExcel()
        {
            if (File.Exists("RobotParameters.xlsx"))
            {
                using (var workbook = new XLWorkbook("RobotParameters.xlsx"))
                {
                    // 导入 RobTargetData
                    var robTargetSheet = workbook.Worksheet("RobTargetData");
                    RobTargetData = new ObservableCollection<RobTargetDataModel>(robTargetSheet.RowsUsed().Skip(1).Select(row => new RobTargetDataModel
                    {
                        Name = row.Cell(1).GetValue<string>(),
                        StorageType = row.Cell(2).GetValue<string>(),
                        DataType = row.Cell(3).GetValue<string>(),
                        X = row.Cell(4).GetValue<string>(),
                        Y = row.Cell(5).GetValue<string>(),
                        Z = row.Cell(6).GetValue<string>(),
                        RZ = row.Cell(7).GetValue<string>(),
                        RY = row.Cell(8).GetValue<string>(),
                        RX = row.Cell(9).GetValue<string>(),
                        IsChecked = row.Cell(10).GetValue<bool>(),
                        ModifyPosition = row.Cell(11).GetValue<string>()
                    }));

                    // 导入 ThreeData
                    var threeDataSheet = workbook.Worksheet("ThreeData");
                    ThreeData = new ObservableCollection<ThreeDataModel>(threeDataSheet.RowsUsed().Skip(1).Select(row => new ThreeDataModel
                    {
                        Name = row.Cell(1).GetValue<string>(),
                        StorageType = row.Cell(2).GetValue<string>(),
                        DataType = row.Cell(3).GetValue<string>(),
                        Value = row.Cell(4).GetValue<string>(),
                        ModifyValue = row.Cell(5).GetValue<string>(),
                        Comment = row.Cell(6).GetValue<string>()
                    }));

                    // 导入 DataTypeArray
                    var dataTypeArraySheet = workbook.Worksheet("DataTypeArray");
                    DataTypeArray = new ObservableCollection<DataTypeArrayModel>(dataTypeArraySheet.RowsUsed().Skip(1).Select(row => new DataTypeArrayModel
                    {
                        Name = row.Cell(1).GetValue<string>(),
                        StorageType = row.Cell(2).GetValue<string>(),
                        DataType = row.Cell(3).GetValue<string>(),
                        Value = row.Cell(4).GetValue<string>(),
                        ModifyValue = row.Cell(5).GetValue<string>(),
                        Comment = row.Cell(6).GetValue<string>()
                    }));
                }
            }
        }
    }
}